First Chapter

GitBook allows you to organize your book into chapters, each chapter is stored in a separate file like this one.

  • Abstract class vs Interface First,Interface only contains methods signatures and fields, not implementations. Abstract class can contain a mix of methods declared with or without an implementation. Second,
  • Overriding vs Overloading

Overriding means having two methods with the same method and parameters. One of the methods if in the parent class and the other is in the child class. Overriding allows a child class to provide a specific implementation of a method that is already provided its parent class Overloading , method name same , can be in the same class, and different parameters.

Static indicates that this method can be called without creating an instance of this class. Without it, you have to instantiate this class and call his method from the resulting object.

Main is the name of a function. main() is special because it is the start of the program

final control whether a variable , method , or class is " change-able" finally used in a try/catch block to ensure that a segment of code is always executed finalize() called by garbage collector once it determins that no more references exist.

Generics vs Templates : Explain the difference between templates in C++ and generics in java?

  • Checked / unchecked exception Checked exceptions are the exceptions that are checked at compile time. throws a checked exception , then the method must either handle the exception or specify the exception using throw keyword. unchecked exception are the exceptions that are not checked at compiled time. In C++, all exceptions are unchecked, so it is not forced by the compiler to either handle or specify the exception. In java exceptions, under Error and RuntimeException classes are unchecked , everything else under throwable is checked.

Hashcode() / equals() Defined in java.lang.Object public boolean equals(Object obj) public int hashCode() The contract between equals() and hashCode() is : 1) If two objects are equal, then they must have the same hash code. 2) if two objects have the same hash code , they may or may not equal So if we want to use HashSet, we need to override both hashcode() and equal()

数据结构的比较和考点

  • Array vs ArrayList vs LinkedList Array is container object that holds a fixed number of values of a single type Array has fix size, can contain primitive types, can be multi-dimensional, while ArrayList is resizable, cannot contain primitive types

ArrayList is implemented by array, LinkedList is implemented by doubley linked list

  • Vector vs ArrayList Vector is synchronized , ArrayList is not. So Vector is slower than ArrayList A Vector is defaults to doubling the size of its array, while the ArrayList increase its array size by 50 percent
  • HashTable is synchronized , HashMap is not.

  • StringBuffer is synchronized , StringBuilder is not

  • String vs StringBuffer

String class is used to manipulate character strings that cannot be changed. Objects of type String are read only and immutable The StringBuffer class is used to represent characters that can be modified

  • HashMap vs TreeMap vs HashTable vs LinkedHashMap vs ConcurrentHashMap

HashMap is implemented as a hash table, and there is no ordering on keys or values. TreeMap is implemented based on red-black tree structure , and it is ordered by the ky LinkedHashMap preserves the insertion order HashTable is synchronized , in constrast to HashMap. It has an overhead for synchronization. All methods of HashTable are synchronized which makes them quite slow due to contention if a number of thread increases ConcurrentHashMap is thread safe without synchronizing the whole map. Reads acn happen very fast while write is done with a lock. There is no locking at the object level. Unlike Hashtable and synchronized map, it never locks whole map, instead , it divides the map into segments and locking is done on those. Though it perfroms better if a number of reader threads are greater than the number of writer threads.

Comparable vs Comparator Comparable is implemented by a class in order to be able to comparing object of itself with some other objects. Comparable -> object . object. Firstly , the class needs to implement Comparable , and in the class need to override compareTo method

public int compareTo(E e)

Comparator , in some situations, you may not want to change a class and make it comparable. In such cases, Comparator can be used if you want to compare objects based on certain attributes/field. We need to write a MyComparator class that implements Comparator interface, then override the compare menthod

public int compare(E e1, E e2)

PriorityQueue(heap) An unbounded priorty queue based on a priority hep not synchronized , otherwize use the thread-safe PrioriryBlockingQueue

HashSet vs TreeSet vs LinkedHashSet Set interface: A set contains no duplicate elements. In brief, if you need a fast set, you should use HashSet; if you need a sorted set, then TreeSet should be used; if you need a set that can be store the insertion order, LinkedHashSet should be used. HashSet is implemented using a hash table. Elements are not ordered. The add, remove, and contains methods have constant time complexity O(1) TreeSet is implemeted using a tree structure (red-black tree). The elements in a set are sorted. but the add , remove contains methods are logn complexity.

results matching ""

    No results matching ""